<template>
  <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" @callBack="onCallBack" @close="onClose">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px" label-position="left">

      <el-form-item label="发票类型" prop="type">
        <el-radio-group v-model="form.type" clearable placeholder="请选择发票类型">
          <el-radio v-for="item in type_options" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="购货方名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入购货方名称" />
      </el-form-item>

      <el-form-item label="纳税人识别号" prop="code">
        <el-input v-model="form.code" clearable placeholder="请输入纳税人识别号" />
      </el-form-item>

      <el-form-item label="购货方邮箱" prop="email">
        <el-input v-model="form.email" clearable placeholder="请输入购货方邮箱" />
      </el-form-item>

      <el-form-item label="总金额" prop="fee">
        <el-input v-model="form.fee" clearable placeholder="请输入总金额" />
      </el-form-item>

      <el-form-item label="收费项目" prop="item">
        <el-select v-model="form.item" placeholder="请选择项目项目" clearable style="width: 100%;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="bz">
        <el-input
          v-model="form.bz"
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息"
        />

      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, save } from '@/api/sysInvoice'

export default {
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      loading: false,
      type_options: [{
        value: 1,
        label: '普通发票'
      }, {
        value: 2,
        label: '增值税专用发票'
      }],
      options: [{
        value: 1,
        label: '服务费'
      }, {
        value: 2,
        label: '代理费'
      },
      {
        value: 3,
        label: '工程量清单及最高投标限价编制费'
      }, {
        value: 4,
        label: '设计费'
      },
      {
        value: 5,
        label: '招标服务费'
      },
      {
        value: 6,
        label: '招标代理费'
      },
      {
        value: 7,
        label: '咨询费'
      }],
      form: {
        id: '',
        name: '',
        type: '',
        code: '',
        email: '',
        fee: '',
        item: 5,
        bz: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请填写收货方名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请填写收货方纳税人识别号',
            trigger: 'blur'
          }
        ],
        fee: [
          {
            required: true,
            message: '请输入开票金额',
            trigger: 'blur'
          }
        ],
        item: [
          {
            required: true,
            message: '请选择收费标准',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: '请填写电子邮箱',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择发票类型',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.id = rtn.id
            this.form.name = rtn.name
            this.form.code = rtn.code
            this.form.type = rtn.type
            this.form.projectName = rtn.projectName
            this.form.email = rtn.email
            this.form.item = rtn.item
            this.form.fee = rtn.fee
            this.form.bz = rtn.bz
          }
        })
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id
        this.title = '编辑'
        this.init()
      } else {
        this.title = '添加'
      }
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        console.log(this.form)
        if (valid) {
          save(this.form)
            .then((res) => {
              console.log(res)
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.onCallBack()
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    onCallBack() {
      this.$emit('onCallBack')
    }
  }
}
</script>

<style scoped>
.el-form {
  padding-left: 30px;
  padding-right: 30px;
}
</style>
